package cn.sx213.mongodb;

import cn.sx213.mongodb.config.MongoDbConfig;
import cn.sx213.mongodb.entity.Employee;
import cn.sx213.mongodb.repository.EmployeeRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.util.List;

/**
 * @Author liliang
 * @Description:
 * @Date 18/1/4 下午4:39
 */
public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(MongoDbConfig.class);
        ctx.refresh();
        EmployeeRepository employeeRepository = ctx.getBean(EmployeeRepository.class);

//        Employee zhangsan = new Employee(100, "张三", 25);
//        Employee lisi = new Employee(101, "李四", 25);
//        Employee wangwu = new Employee(102, "王五", 25);
//        Employee zhaoliu = new Employee(103, "赵六", 25);
//        Employee qiqi = new Employee(104, "七七", 20);
//
//        employeeRepository.save(zhangsan);
//        employeeRepository.save(lisi);
//        employeeRepository.save(wangwu);
//        employeeRepository.save(zhaoliu);
//        employeeRepository.save(qiqi);
//        employeeRepository.deleteAll();
        long count = employeeRepository.count();
        System.out.println(count);
        Page<Employee> employeePage = employeeRepository.findAll(new PageRequest(0, 2));
        System.out.println(employeePage.hasNext());
        employeePage.forEach(e -> System.out.println("ID:" + e.getId() + ", name:" + e.getName() + ", age:" + e.getAge()));

        List<Employee> employeeList = employeeRepository.findAll();
        employeeList.forEach(e -> System.out.println("ID:" + e.getId() + ", name:" + e.getName() + ", age:" + e.getAge()));
    }
}
